package com.jsql.view.swing.panel.preferences;

import com.jsql.util.LogLevelUtil;
import com.jsql.util.StringUtil;
import com.jsql.view.swing.text.SyntaxTextArea;
import com.jsql.view.swing.text.listener.DocumentListenerEditing;
import com.jsql.view.swing.util.MediatorHelper;
import com.jsql.view.swing.util.UiUtil;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rtextarea.RTextScrollPane;
import org.yaml.snakeyaml.parser.ParserException;
import org.yaml.snakeyaml.scanner.ScannerException;

import javax.swing.*;

public class PanelExploit extends JPanel {

    private static final SyntaxTextArea textareaRevshells = new SyntaxTextArea("Reverse shell list");
    private static final String TITLE = "Reverse shell server (connector)";
    private static final String PATTERN = "<html><b>%s</b></html>";

    public PanelExploit() {
        PanelExploit.textareaRevshells.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_YAML);
        PanelExploit.applyTheme();

        var preferencesUtil = MediatorHelper.model().getMediatorUtils().getPreferencesUtil();
        var labelOrigin = new JLabel(String.format(PanelExploit.PATTERN, PanelExploit.TITLE));
        PanelExploit.textareaRevshells.getDocument().addDocumentListener(new DocumentListenerEditing() {
            @Override
            public void process() {
                try {
                    preferencesUtil.parseReverseCommands(PanelExploit.textareaRevshells.getText());
                    labelOrigin.setText(String.format(PanelExploit.PATTERN, PanelExploit.TITLE));
                } catch (
                    ParserException | ScannerException  // yml
                    | NullPointerException  // map missing key
                    | ClassCastException  // incorrect model
                    e
                ) {
                    labelOrigin.setText(String.format(PanelExploit.PATTERN, StringUtil.formatReport(
                        LogLevelUtil.COLOR_RED,
                        PanelExploit.TITLE
                        + ", YAML must follow the name and command template :<br>"
                        // Fix #96073: NullPointerException on e.getMessage().replace()
                        + e.toString().replace("\n", "<br>")  // toString to prevent NPE (jdk11)
                    )));
                }
            }
        });
        PanelExploit.textareaRevshells.setText(
            preferencesUtil.getCommandsReverseYaml().replace("\r\n", "\n")  // required to prevent \ra\n issue
        );
        PanelExploit.textareaRevshells.setCaretPosition(0);

        var scrollPane = new RTextScrollPane(PanelExploit.textareaRevshells);

        labelOrigin.setBorder(PanelGeneral.MARGIN);

        var groupLayout = new GroupLayout(this);
        this.setLayout(groupLayout);

        groupLayout.setHorizontalGroup(
            groupLayout
            .createSequentialGroup()
            .addGroup(
                groupLayout
                .createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(labelOrigin)
                .addComponent(scrollPane)
            )
        );

        groupLayout.setVerticalGroup(
            groupLayout
            .createSequentialGroup()
            .addGroup(
                groupLayout
                .createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(labelOrigin)
            )
            .addGroup(
                groupLayout
                .createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(scrollPane)
            )
        );
    }

    public static void applyTheme() {
        UiUtil.applySyntaxTheme(PanelExploit.textareaRevshells);
    }
}
